<?php
include_once SCRIPT_FILENAME.'/Zend_Mail/Mail.php';

class MyMail extends Zend_Mail{

    public function __construct(){
        parent::__construct('UTF-8');
        $this->setDate(date("d.m.Y H:i:s"));
    }

    public function clearAll(){
        $this->clearFrom();
        $this->clearSubject();
        $this->clearRecipients();
    }

    /*
     * purpuse: used for testing cronjob
     */
    public function mailTest(){
        $this->clearAll();
        //sende eine mail an an die klinik
        $this->setBodyText('cronjob läuft');
        //$this->setBodyHtml($this->parseContent(file_get_contents (BOOTSTRAP_EMAIL_PATH.'account'.$option.'Body.html'), $clinicData[0]));
        $this->setFrom('akrug@webpages.de', 'el berto');
        $this->addTo('akrug@webpages.de', 'el berto');
        //$this->addTo($clinicData[0]['email'], $clinicData[0]['klinik_name']);
        $this->setSubject('testmail');

        $this->send();

        return true;
    }

    /*
     * option: FirstMail - mail an klinik mit verlaengerungslink
     *         SecondMail - mail an klinik mit verlaengerungslink 
     *         LastMail - mail an klinik ohne verlaengerungsmail
     * 
     */ 
    public function mailSituations($clinicData = null, $option = null){

        //wenn es kein body oder header gibt, wird die mail nicht versendet
        if(!is_file(EMAIL_PATH.$option.'Body.html') || !is_file(EMAIL_PATH.$option.'Header.txt')){
            return false;
        }

        if(NULL === $option){
            return false;
        }

        if('FirstMail' == $option || 'SecondMail' == $option){
        	//erstelle link, der das angebot um 3 monate verlaengert
        	$clinicData['link'] = $this->createLink($clinicData['angebot_nid']);
        }

        $this->clearAll();
        //sende eine mail an an die klinik
        $this->setBodyHtml($this->parseContent(file_get_contents (EMAIL_PATH.$option.'Body.html'), $clinicData));
        //$this->setBodyHtml($this->parseContent(file_get_contents (BOOTSTRAP_EMAIL_PATH.'account'.$option.'Body.html'), $clinicData[0]));
        $this->setFrom('info@arzt-karriere.eu', 'Arzt + Karriere');
        $this->addTo('akrug@webpages.de', $clinicData[0]['klinik_name']);
        //$this->addTo($clinicData[0]['email'], $clinicData[0]['klinik_name']);
        $this->setSubject($this->parseContent(file_get_contents (EMAIL_PATH.$option.'Header.txt'), $clinicData));

        $this->send();

        return true;
    }

    public function createLink($angebot_nid){

    	$link = URI;

    	$link .= 'renew/';

    	//hier wird die angebot_nid angehaengt
    	$link .= $angebot_nid.'/';

    	//einsalt wird angehaengt
    	$link .= '/'.md5($angebot_nid.SALT);

    	return $link;
    }

    public function parseContent($content, $variables){

        //fuege baseurl hinzu
        $variables['BASE_URL'] = URI;
        $variables['dateNow'] = date('d.m.Y');

        foreach ($variables as $vIndex => $v){
            $content = str_replace('###'.$vIndex.'###', utf8_encode($v), $content);
        }
        return $content;
    }
}
?>